<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class userAction extends publicAction
{
    public function _empty()
    {
        header("HTTP/1.0 404 Not Found");
        $this->display('public:404');
    }
    public function findPassword(){
        $this->display();
    }
    public function register(){
        if(!empty($_POST)){
            $data['username']=$_POST['username'];
            $data['password']=md5($_POST['password']);
            $data['email']=$_POST['email'];
            $data['register_time']=date('Y-m-d H:i:s',time());
            $verify=md5($_POST['verify']);
            $dataRetrun=array('flag'=>'false','msg'=>'false');//ajax返回信息
            if($_SESSION['verify']==$verify){
                $dataRetrun['flag']='success';
            }else{
                $this->ajaxReturn($dataRetrun,'JSON');
                exit();
            }
            $user=D('user')->data($data)->add();
            if($user){
                $dataRetrun['msg']='success';
                $this->ajaxReturn($dataRetrun,'JSON');
                exit();
            }else{
                $this->ajaxReturn($dataRetrun,'JSON');
                exit(); 
            }
        }
        $this->display();
    }
    public function login(){
       if($_POST['username']&&$_POST['password']){
            $username=$_POST['username'];
            $password=md5($_POST['password']);
            if($_POST['verify']){
                $verify=md5($_POST['verify']);
                 if($_SESSION['verify']!=$verify){
                    $this->assign('error','验证码输入错误');
                    $this->display();
                    exit();
                }
            }
            $whereMap=array('username'=>$username,'password'=>$password,'state'=>'active');
            $userInfo=D('user')->where($whereMap)->find();        
            if(!$userInfo){
                $this->assign('msg','用户名或密码错误');
                $this->display();
                exit();
            }else{
                D('user')->where("id={$userInfo['id']}")->setInc('credits',5);
                D('user')->where("id={$userInfo['id']}")->setField('is_online','online');
                D('user')->where("id={$userInfo['id']}")->setField('login_time',date('Y-m-d H:i:s',time()));
                session('userInfo',$userInfo);
                unset($_SESSION['userInfo']['password']);
                $_SESSION['userInfo']['credits']+=5;
                $this->assign('userInfo',session('userInfo'));
                redirect('/User/forum/forum');   
            }
        }else{
            if(isset($_SESSION['userInfo'])){
                $this->assign('userInfo',session('userInfo'));
                redirect('/User/forum/forum');
            }else{
                 $this->display();
            }
        }
       
    }
    public function userEdit(){
        $id=$_POST['id'];
        unset($_POST['id']);
        $data=$_POST;
        $res=M('user')->where("id=$id")->data($data)->save();
        if($res){
            redirect("userInfo?id=$id");
        }else{
            redirect("userInfo?id=$id&flag=error");
        }
    }
    public function changePassword(){
         $this->assign('username',$_SESSION['username']);
         unset($_SESSION['username']);
        if(!empty($_POST)){
            $whereMap=array('username'=>$_POST['username']);
            $password=md5($_POST['new_password']);
            $res=D('user')->where($whereMap)->data("password={$password}")->save();
            if($res){
                $this->assign('msg',"<font color='green'>修改密码成功</font>");
                $this->display();
                exit();
            }else{
                $this->assign('msg',"<font color='red'>修改密码失败</font>");
                $this->display();
                exit();
            }
        }
        $this->display();
    }
    public function findByField(){
        if(!empty($_POST)){
           $username=$_POST['username'];
           $email=$_POST['email'];
           $verify=md5($_POST['verify']);
           $whereMap=array('username'=>$username,'email'=>$email);
           $res=D('user')->where($whereMap)->find();
           if($res&&$_SESSION['verify']==$verify){
               session('username',$res['username']);
               redirect('changePassword');
               exit();
           }else{
               $this->assign('error',"<font color='red'>验证失败</font>");
               $this->display('findPassword');
           }
        }
    }
    public function findByName(){
        if($_POST['username']){
             $dataRetrun=array('flag'=>'false');//ajax返回信息
             $whereMap=array('username'=>$_POST['username']);
             $isExist=D('user')->where($whereMap)->find();
             if(empty($isExist)){
                 $dataRetrun['flag']='success';
                 $this->ajaxReturn($dataRetrun,'JSON');
                 exit();
             }else{
                 $this->ajaxReturn($dataRetrun,'JSON');
                 exit();
             }
        }  
    }
    public function userInfo(){
        if($_GET['id']){
            $whereMap=array('id'=>$_GET['id']);
            $user=D('user')->where($whereMap)->find(); 
            $this->assign('user',$user);
            $this->display();exit();
        }
        $this->display();
    }
    public function logout(){
        D('user')->where("id={$_SESSION['userInfo']['id']}")->setField('is_online','outline');
        session(null);
        redirect('/User/forum/forum');
    }
}
?>
